package com.hwh.traffic.fragment;


import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hwh.traffic.R;
import com.hwh.traffic.noticeEntity.Items;
import com.hwh.traffic.noticeEntity.NoticeDomJson;
import com.hwh.traffic.utils.DensityUtil;
import com.hwh.traffic.utils.HttpUtil;

import java.io.IOException;


public class NoticeFragment extends Fragment {
    private static ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    private TextView mTitleText;
    private LinearLayout mNocice_list;
    private WebView webView;

    public NoticeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_notice, container, false);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //加载动画
        View avi = getView().findViewById(R.id.avi);
        webView = getView().findViewById(R.id.web_page);
        new Thread(() -> {
            String notice_api = "https://app.ibuscloud.com/v1/announcement/getAnnouncement?city=350100&h5Platform=7&token=A90D8DAFD7A63935BC56F082D31995770F9A1874F4ECDC4A4A4FE973AF616F95850A7CE2837E778DC3F45D57AC1C464E&typeCode=2&page=1&count=50&appSource=com.ibuscloud.fuzhou&radius=1000&lat=26.074268&lng=119.296389&uuid=fbdb87b5-1ea9-3259-87f3-69ceeefdde31";
            String notice_json_str = HttpUtil.httpGet(notice_api);
            try {
                mNocice_list = getView().findViewById(R.id.notice_list);
                NoticeDomJson noticeDomJson = MAPPER.readValue(notice_json_str, NoticeDomJson.class);
                getActivity().runOnUiThread(() -> {
                    //渲染所有公告信息
                    for (Items item : noticeDomJson.getItems()) {
                        LinearLayout mLinearLayout = new LinearLayout(getActivity().getApplicationContext());
                        ImageView mImageView = new ImageView(getActivity().getApplicationContext());
                        TextView mTextView = new TextView(getActivity().getApplicationContext());
                        //设置10dp
                        int dp_10 = DensityUtil.dip2px(getActivity().getApplicationContext(), 10);
                        mNocice_list.addView(mLinearLayout);
                        mLinearLayout.addView(mImageView);
                        mLinearLayout.addView(mTextView);

                        //设置LinearLayout 布局
                        ViewGroup.LayoutParams lp_LinearLayout = mLinearLayout.getLayoutParams();
                        lp_LinearLayout.width = ViewGroup.LayoutParams.MATCH_PARENT;
                        lp_LinearLayout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
                        mLinearLayout.setPadding(dp_10, dp_10, dp_10, dp_10);
                        mLinearLayout.setBackgroundColor(getResources().getColor(R.color.main_bg));
                        mLinearLayout.setLayoutParams(lp_LinearLayout);

                        ViewGroup.LayoutParams lp_ImageView = mImageView.getLayoutParams();
                        lp_ImageView.width = ViewGroup.LayoutParams.MATCH_PARENT;
                        lp_ImageView.height = ViewGroup.LayoutParams.MATCH_PARENT;
                        mImageView.setImageDrawable(getResources().getDrawable(R.drawable.notice_img));
                        mImageView.setLayoutParams(lp_ImageView);
                        mImageView.setOnClickListener(v -> {
                            String url = item.getH5();
                            Bundle bundle = new Bundle();
                            bundle.putString("url",url);
                            NoticeH5Fragment web = new NoticeH5Fragment();
                            web.setArguments(bundle);
                            getFragmentManager().beginTransaction().replace(R.id.notice_page,web).addToBackStack(null).commit();

                        });

                        LinearLayout.LayoutParams lp_TextView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
                        lp_TextView.height = ViewGroup.LayoutParams.MATCH_PARENT;
                        lp_TextView.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                        mTextView.setTextColor(getResources().getColor(R.color.black));
                        mTextView.setTextSize(15);
                        mTextView.setPadding(dp_10, dp_10, dp_10, dp_10);
                        mTextView.setText(item.getTitle());
                        mTextView.setLayoutParams(lp_TextView);
                    }
                    //停止加载动画
                    avi.setVisibility(View.GONE);
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        mTitleText = getView().findViewById(R.id.text_title);
        mTitleText.setText("资讯公告");





    }
}
